package com.company.teddy;

import com.company.json.Floor;
import com.company.json.Json;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Created by v on 17-4-23.
 */
public class Extract {
    //利用里面的元素读取elements
    public static void extract() throws IOException{
        HashMap<String,List<String>> map=Read.ReadSign();
        File[] files=Read.ReadHTML();
        List<String> urls=Read.ReadURL();

        for(int i=0;i<files.length;i++){
            analyze(map,files[i],urls);
        }
    }

    //对单一一个file进行
    public static void analyze(HashMap<String,List<String>> map,
                               File file,List<String> urls) throws IOException{
        int index=Integer.valueOf(file.getName());
        String url=urls.get(index);

        List<String> attributes=map.get(url);

        Document doc=Jsoup.parse(file,"utf-8");
        Element body=doc.body();

        Iterator<String> iterator=attributes.iterator();
        String attr=iterator.next();
        String value=iterator.next();

        Json json=new Json(title(doc,attr,value));
        Floor host=new Floor();

        host.setText(host(body,iterator.next(),iterator.next()));
        host.setAuthorName(host(body,iterator.next(),iterator.next()));
        host.setTime(host(body,iterator.next(),iterator.next()));

        List<Floor> list=reply(body,iterator.next(),iterator.next(),iterator.next(),
                iterator.next(),iterator.next(),iterator.next());

        json.setHost(host);
        json.setList(list);

        json.outPut();
    }

    //针对title进行的提取.
    public static String title(Document doc,String attr,String value){
        String title=doc.title();
        if(attr.equals("null")){
            return title;
        }

        Elements elements=doc.body().getElementsByAttributeValue(attr,value);

        if(elements.size()!=1){
            return title;
        }

        return elements.get(0).ownText();
    }

    //针对楼层进行提取
    public static List<Floor> reply(Element body,
                                    String attr1,String value1,
                                    String attr2,String value2,
                                    String attr3,String value3){
        List<Floor> floors=new ArrayList<>();
        Elements elements1=body.getElementsByAttributeValueContaining(attr1,value1);
        Elements elements2=body.getElementsByAttributeValueContaining(attr2,value2);
        Elements elements3=body.getElementsByAttributeValueContaining(attr3,value3);

        int max=Math.max(elements1.size(),elements2.size());
        max=Math.max(max,elements3.size());

        for(int i=0;i<max;i++){
            floors.add(new Floor());
        }

        for(int i=0;i<elements1.size();i++){
            floors.get(i).setText(elements1.get(i).ownText());
        }
        for(int i=0;i<elements2.size();i++){
            floors.get(i).setAuthorName(elements2.get(i).ownText());
        }
        for(int i=0;i<elements3.size();i++){
            floors.get(i).setAuthorName(elements3.get(i).ownText());
        }

        return floors;
    }

    //针对一层楼的定点提取
    public static String host(Element body,String attr,String value){
        if(attr.equals("null")){
            return "";
        }

        Elements elements=body.getElementsByAttributeValue(attr,value);

        if(elements==null){
            return "";
        }else {
            return elements.get(0).ownText();
        }
    }
}
